<?php
	session_start();
	require 'php_mailer\PHPMailer-5.2.6\class.phpmailer.php';
	 require("php_mailer\PHPMailer-5.2.6\class.smtp.php");
	require_once "db.php";
	include "header.php";
	//fiktivni prmenlivi za da stava po-lesno sled tova promqnata vkoda
	$hallid=1;
	$prid=1;
?>

<?php 
	if (isset($_SESSION['uisadmin']) && $_SESSION['uisadmin'] >-1){ //user is logged
		if(!isset($_GET["prid"])){
			$_GET["prid"]=$_POST["hid"];
	}
	$projid=$_GET["prid"];
	$quer=" select  p.hall_id as hall_id, h.seat_count from projection p join halls h on (h.id=p.hall_id) where p.id=".$_GET["prid"];
	$res= db_query($quer) or pretty_die("problem reading table z".mysql_error());
	$r=db_get_row($res);
	$id=$r["hall_id"];
	$seats=$r["seat_count"];
	$hallid=$r["hall_id"];
	$prid=$_GET["prid"];
	$seatcount="Select seat_count from halls where id=".$hallid;
	$res1=db_query($seatcount);
	$seatres = db_get_row($res1);
	for($i=1;$i<=$seatres["seat_count"];$i++){
		$takenseats="select seat,projection_id from reservation where projection_id=".$prid." and seat=".$i;
		$restaken=db_query($takenseats);
		$seatsrow=db_get_row($restaken);
		
		if (isset($seatsrow['seat'])){
			$taken[$seatsrow['seat']]="";
		}
		else $taken[$i]="<input type='checkbox' name=".$i.">";
		
	
	}
	if (isset($_POST['Reserve'])){
		$message="";
		for($i=1;$i<=$seatres["seat_count"];$i++)
			if(isset($_POST[$i])){
				//echo $i;
				$message=$message." seat number:".$i;
				//echo $_SESSION['username'];
				$quer="insert into reservation values(\"".$_SESSION['username']."\",".$prid.",".$i.")";
				$dbrec=db_query($quer)
				or pretty_die("problem reading table2".mysql_error());
				$taken[$i]="";
			}
			//tuk da napi6aza izpra6tane na email
			$quer="select * from users where username=\"".$_SESSION['username']." \" ";
				$dbrec=db_query($quer)
				or pretty_die("problem reading table2".mysql_error());
				$mailman=db_get_row($dbrec);
	
			$quer="select h.name as hall,c.name as cinema from halls h join cinemas c on (h.cinema_id=c.id) where h.id=\"".$id."\"";
				$dbrec=db_query($quer)
				or pretty_die("problem reading table2".mysql_error());
				$cinema_mail=db_get_row($dbrec);
	
			$quer="select m.name as filmname,p.date from projection p join movies m on(p.film_id=m.id) where p.id=\"".$_GET["prid"]."\"";
				$dbrec=db_query($quer)
				or pretty_die("problem reading table2".mysql_error());
				$film_mail=db_get_row($dbrec);
	
			$mail=new PHPMailer();
			//$mail->SMTPDebug = 1;
			$mail->Username="phpproekt@gmail.com";
			$mail->Password='9212133425';
			$mail->AddAddress($mailman["email"]);
			$mail->FromName="CMS Admin";
			$mail->Subject = 'Ticket Reservation';
			$mail->Body = "You have succesfully reserved ".$message." at hall ".$cinema_mail["hall"]." in cinema: ".$cinema_mail["cinema"]." for the movie: ".$film_mail["filmname"]." at ".$film_mail["date"];
			$mail->Host = 'ssl://smtp.gmail.com';
			$mail->Port = 465;
			$mail->IsSMTP(); // enable SMTP
			$mail->SMTPAuth = true;
			$mail->From="phpproekt@gmail.com";
			$mail->SetFrom("phpproekt@gmail.com", "phpproekt@gmail.com");
			if(!$mail->Send()){
				echo "err: ".$mail->ErrorInfo;
			}
			else {
				echo 'You have successfully made a reservation!';
			}
			//do tuk s emaila
	}
	echo "<form method='post' action=".$_SERVER["PHP_SELF"].">";
	echo "<div id=saloon>";
	echo "<table border='1' >";
	for($i=1;$i<=$seatres["seat_count"];$i++){
		
			if($taken[$i]=="<input type='checkbox' name=".$i.">"){
				$seat='img/green_seat.png';
			}
			else $seat='img/red_seat.png';
		if($i%10==1){
			echo"<tr><td style=\"background-image:url($seat); width:75px; height:75px;\">".$i."<br/>".$taken[$i]."</td>";
		}
		elseif($i%10==0){
			echo"<td style=\"background-image:url($seat); width:75px; height:75px;\">".$i."<br/>".$taken[$i]."</td></tr>";
		}
		elseif($i%10==5){
			echo"<td style=\"background-image:url($seat); width:75px; height:75px;\">".$i."<br/>".$taken[$i]."</td><td> &nbsp &nbsp &nbsp</td>";
		}
		else{
			echo"<td style=\"background-image:url($seat); width:75px; height:75px;\">".$i."<br/>".$taken[$i]."</td>";
		}
	}
	echo "</table>";
	echo"</div>";
	echo "<input type='hidden' name='hid' value='$prid'/>";
	echo "<input type='submit' name='Reserve' value='Reserve'/>";
	echo "</form>";
}
else{
	echo "If you want to make a reservation you should log in!<br/>";
	?>
	<a href= "login.php"> LOGIN PLS</a>
	<?php 
}
?>

<?php 
include "footer.php";
?>